package com.yonyou.pmclouds.basecom.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class StorageFactory {

    @Autowired
    private NCCFileStorageUtil nccFileStorageUtil;
    @Autowired
    private OssUtil ossUtil;

    private StorageType storageType;

    @Value("${storage.type}")
    private void setStorageType(String storageType){
        this.storageType = StorageType.get(storageType);
    }

    public StorageType getStorageType(){
        return storageType;
    }

    public AbstractStorageUtil getStorageUtil(){
        return storageType == StorageType.ncc ? nccFileStorageUtil : ossUtil;
    }

    public enum StorageType{
        oss, ncc;

        public static StorageType get(String storageType){
            if(storageType == null || "".equals(storageType.trim()) || oss.name().equalsIgnoreCase(storageType)) {
                return oss;
            } else if(ncc.name().equalsIgnoreCase(storageType)) {
                return ncc;
            } else {
                throw new RuntimeException("Configuration error, can't find proper storage server.");
            }
        }
    }
}
